package com.medicine.hbp.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.widget.RoundImageView;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medicine.hbp.adapter.FragmentAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.fragment.myuser.FragmentUser01;
import com.medicine.hbp.fragment.myuser.FragmentUser02;
import com.medicine.hbp.fragment.myuser.FragmentUser03;
import com.medicine.hbp.fragment.myuser.FragmentUser04;
import com.medicine.hbp.fragment.myuser.FragmentUser05;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.UserDetailsVo;
import com.medicine.hbp.mob.UserInfoVo;
import com.medicine.hbp.page.ViewPageIndexMe;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

public class SelfUserTwoActivity extends BaseActivity {
	@BindView(id = R.id.view_page, click = false)
	public ViewPager mViewPage;
	@BindView(id = R.id.btn_txt_back, click = true)
	public TextView btn_txt_back;
	@BindView(id = R.id.btn_txt_set, click = true)
	public TextView btn_txt_set;

	@BindView(id = R.id.ll_tg, click = true)
	public LinearLayout ll_tg;
	@BindView(id = R.id.ll_tousu, click = true)
	public LinearLayout ll_tousu;

	@BindView(id = R.id.text_user_name, click = false)
	public TextView text_user_name;// 姓名
	@BindView(id = R.id.text_user_qianm, click = false)
	public TextView text_user_qianm;// 签名
	@BindView(id = R.id.text_user_address, click = false)
	public TextView text_user_address;// 地址
	@BindView(id = R.id.text_user_jifen, click = false)
	public TextView text_user_jifen;// 积分
	@BindView(id = R.id.text_user_fs, click = false)
	public TextView text_user_fs;// 粉丝
	@BindView(id = R.id.text_user_gz, click = false)
	public TextView text_user_gz;// 关注

	@BindView(id = R.id.btn_guanz, click = true)
	public Button btn_guanz;// 关注按钮
	@BindView(id = R.id.btn_a, click = true)
	public Button btn_a;// @我
	@BindView(id = R.id.txt_a_red)
	public TextView txt_a_red;// 小红点
	@BindView(id = R.id.img_userhead, click = true)
	public RoundImageView img_userhead;// 用户头像

	@BindView(id = R.id.txt_jifen, click = true)
	public TextView txt_jifen;// 积分规则
	private List<String> title2 = Arrays.asList("收藏", "投稿", "评论", "关注", "粉丝");
	private List<String> title3 = Arrays.asList("收藏", "投稿", "评论", "关注", "粉丝");
	private int userId;
	/**
	 * 用户信息返回对象
	 */
	public UserInfoVo userInfo;

	@BindView(id = R.id.indicator, click = false)
	public ViewPageIndexMe indicator;

	public static SelfUserTwoActivity activity;
	private int type=0;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_self_usertwo);

	}

	@Override
	public void widgetClick(View v) {
		Intent intent = null;
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_txt_back:
				finish();
				break;
			case R.id.img_userhead:
				if (type==1) {// 用户个人信息
					intent = new Intent(aty, UserSetActivity.class);
				}
				break;
			case R.id.btn_txt_set://我的钱包
//			intent = new Intent(aty, SettingActivity.class);
				intent=new Intent(aty,MyMoneyActivity.class);
				break;
			case R.id.ll_tg:// 我要投稿
				StartIntent.startRelease(aty);
				break;
			case R.id.txt_jifen:// 我要投稿
				intent = new Intent(aty, IntegralActivity.class);
				break;
			case R.id.ll_tousu:// 我要投诉
				intent = new Intent(aty, AdviceActivity.class);
				break;
			case R.id.btn_guanz:// 关注
				if (userInfo.isAtt == 0) {
					httpAttSubmit();
				} else {
					httpDelattuser();
				}
				break;
			case R.id.btn_a:// @我
				if (LoginSp.getUserId(aty) == userId) {// 用户个人信息
					intent = new Intent(aty, UserListActivity.class);
					intent.putExtra("UserId", userId);
				} else {// 其他用户信息
					intent = new Intent(aty, ArtMeActivity.class);
					intent.putExtra("UserId", userId);
				}
				break;
		}
		if (intent != null) {
			startActivity(intent);
		}
	}

	@Override
	public void initWidget() {
		super.initWidget();
		txt_jifen.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); // 下划线
		txt_jifen.getPaint().setAntiAlias(true);// 抗锯齿
		txt_a_red.setVisibility(View.GONE);
		userId = getIntent().getIntExtra("userId", -99);
		List<Fragment> listf = new ArrayList<Fragment>();
		if (-99 == userId||userId==LoginSp.getUserId(aty)) {// 用户个人信息
			btn_guanz.setVisibility(View.GONE);
		}else{
			btn_guanz.setVisibility(View.VISIBLE);
		}
		if (-99 == userId) {// 用户个人信息
			type=1;
			activity=SelfUserTwoActivity.this;
			userId=LoginSp.getUserId(aty);
			// httpUserDetails();
			btn_txt_back.setVisibility(View.VISIBLE);
			Bundle bundle = new Bundle();
			bundle.putInt("userId", -1);
			FragmentUser01 fragment01 = new FragmentUser01();
			fragment01.setArguments(bundle);
			listf.add(fragment01);
			FragmentUser02 fragment02 = new FragmentUser02();
			fragment02.setArguments(bundle);
			listf.add(fragment02);
			FragmentUser03 fragment03 = new FragmentUser03();
			fragment03.setArguments(bundle);
			listf.add(fragment03);
			FragmentUser04 fragment04 = new FragmentUser04();
			fragment04.setArguments(bundle);
			listf.add(fragment04);
			FragmentUser05 fragment05 = new FragmentUser05();
			fragment05.setArguments(bundle);
			listf.add(fragment05);
			// Fragment fragmentgx = new GxFragment();
			// listf.add(fragmentgx);
			FragmentAdapter adapter = new FragmentAdapter(this.getSupportFragmentManager(), listf);
			mViewPage.setAdapter(adapter);
			mViewPage.setCurrentItem(0);
//			mViewPage.setOffscreenPageLimit(4);
			indicator.setNum(title3.size());
			indicator.setTabItemTitle(title3);
			indicator.setViewPageItem(mViewPage);
		} else {// 其他用户信息
			type=0;
			Bundle bundle = new Bundle();
			bundle.putInt("userId", userId);
			FragmentUser01 fragment01 = new FragmentUser01();
			fragment01.setArguments(bundle);
			listf.add(fragment01);
			FragmentUser02 fragment02 = new FragmentUser02();
			fragment02.setArguments(bundle);
			listf.add(fragment02);
			FragmentUser03 fragment03 = new FragmentUser03();
			fragment03.setArguments(bundle);
			listf.add(fragment03);
			FragmentUser04 fragment04 = new FragmentUser04();
			fragment04.setArguments(bundle);
			listf.add(fragment04);
			FragmentUser05 fragment05 = new FragmentUser05();
			fragment05.setArguments(bundle);
			listf.add(fragment05);

			btn_txt_back.setVisibility(View.VISIBLE);
			btn_txt_set.setVisibility(View.GONE);
			// httpFindUserDetails(userId);
			FragmentAdapter adapter = new FragmentAdapter(
					this.getSupportFragmentManager(), listf);
			mViewPage.setAdapter(adapter);
			mViewPage.setCurrentItem(0);
//			mViewPage.setOffscreenPageLimit(3);
			indicator.setNum(title2.size());
			indicator.setTabItemTitle(title2);
			indicator.setViewPageItem(mViewPage);
		}
		btn_a.setVisibility(View.GONE);
		// btn_a.setText("@我");
	}
	@Override
	protected void onResume() {
		super.onResume();
//		if (0 == userId) {
//			httpUserDetails();
//		} else {
		httpFindUserDetails(userId);
//		}
	}
	KJBitmap bitmap=new KJBitmap();
	/**
	 * 用户自身相关信息获取
	 */
	private void httpUserDetails() {
		HttpParams params = new HttpParams();
		params.put("userId", LoginSp.getUserId(aty));
		AppHttps.HttpPost(this, HttpInterface.FIND_USER_DETAILS, params,
				new AppHttpCallBack<UserDetailsVo>(UserDetailsVo.class, aty) {
					@Override
					public void onSuccess(UserDetailsVo result) {
						if (result.userInfo != null) {
							userInfo = result.userInfo;
							text_user_name.setText(result.userInfo.userName);
							text_user_qianm.setText(result.userInfo.userSignature);
							text_user_address.setText(result.userInfo.userArea);
							text_user_jifen.setText(result.userInfo.userIntegral+ "积分");
							text_user_fs.setText(result.userInfo.userFansCount+ "粉丝");
							text_user_gz.setText(result.userInfo.userAttCount+ "关注");

							LoginSp.setUserHead(result.userInfo.userHeadImg, aty);
							LoginSp.setUserSignature( result.userInfo.userSignature, aty);
							LoginSp.setUserName(result.userInfo.userName, aty);
							if(!TextUtils.isEmpty(result.userInfo.userHeadImg)){
								bitmap.display(img_userhead, result.userInfo.userHeadImg);
							}else{
								img_userhead.setImageResource(R.drawable.img_head_mr);
							}
						}
						// if(userInfo.isMessage==0){
						// txt_a_red.setVisibility(View.GONE);
						// }else{
						// txt_a_red.setVisibility(View.VISIBLE);
						// txt_a_red.setText(userInfo.isMessage+"");
						// }
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}
	/**
	 * 其他用户相关信息获取
	 */
	private void httpFindUserDetails(int id) {
		HttpParams params = new HttpParams();
		params.put("userId", id);
		AppHttps.HttpPost(this, HttpInterface.FIND_USER_DETAILS, params,
				new AppHttpCallBack<UserDetailsVo>(UserDetailsVo.class, aty) {
					@Override
					public void onSuccess(UserDetailsVo result) {
						if (result.userInfo != null) {
							userInfo = result.userInfo;
							text_user_name.setText(result.userInfo.userName);
							if (TextUtils.isEmpty(result.userInfo.userSignature)) {
								text_user_qianm.setText("什么都没有留下");
							} else {
								text_user_qianm.setText(result.userInfo.userSignature);
							}
							text_user_address.setText(result.userInfo.userArea);
//							text_user_jifen.setText(result.userInfo.userIntegral+ "积分");
							text_user_fs.setText(result.userInfo.userFansCount+ "粉丝");
							text_user_gz.setText(result.userInfo.userAttCount+ "关注");
							if (userInfo.isAtt == 0) {
								btn_guanz.setText("加关注");
							} else {
								btn_guanz.setText("取消关注");
							}
							if(!TextUtils.isEmpty(result.userInfo.userHeadImg)){
								bitmap.display(img_userhead, result.userInfo.userHeadImg);
							}else{
								img_userhead.setImageResource(R.mipmap.img_head_wdl);
							}
						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	/**
	 * 用于关注其他用户
	 */
	private void httpAttSubmit() {
		HttpParams params = new HttpParams();
		if (null != userInfo) {
			params.put("userId", userInfo.userId);
		}
		AppHttps.HttpPost(this, HttpInterface.ATT_SUBMIT, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						userInfo.isAtt = userInfo.userId;
						ToastUtil.showShort(aty, "关注成功");
						btn_guanz.setText("取消关注");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	private void httpDelattuser() {
		HttpParams params = new HttpParams();
		if (null != userInfo) {
			params.put("userId", userInfo.userId);
		}
		AppHttps.HttpPost(this, HttpInterface.DEL_ATTUSER, params,
				new AppHttpCallBack<Object>(Object.class) {
					@Override
					public void onSuccess(Object result) {
						userInfo.isAtt = 0;
						ToastUtil.showShort(aty, "取消关注成功");
						btn_guanz.setText("+关注");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			return false;
//		}
//		return super.onKeyDown(keyCode, event);
//	}

}
